#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using std::cout;
using std::endl;
using std::for_each;
using std::vector;
using std::copy;
using std::ostream_iterator;

//一元函数
void print(int &value)
{
    ++value;
    cout << value << "  ";
}

void test()
{
    int a = 10;
    vector<int> vec = {1, 4, 8, 6, 7, 4};
    /* for_each(vec.begin(), vec.end(), print); */
    
    //lambda表达式 === 匿名函数
    //[],捕获列表
    //函数的返回类型写在参数列表右小括号与函数体的左大括号之间,
    //函数的返回类型是可以省略的
    /* for_each(vec.begin(), vec.end(), [&a](int &value) -> void { */
    /*          cout << a << endl; */
    /*          ++a; */
    /*          ++value; */
    /*          cout << value << "  "; */
    /*          }); */
    for_each(vec.begin(), vec.end(), [](int &value) {
             ++value;
             cout << value << "  ";
             });
    cout << endl;
    copy(vec.begin(), vec.end(), 
         ostream_iterator<int>(cout, "  "));
    cout << endl;
}

void display(int value)
{
    cout << value << "  ";
}

void test2()
{
    vector<int> vec = {1, 4, 8, 6, 7, 4};
    for_each(vec.begin(), vec.end(), display);

    cout << endl << endl << "算法中的查找函数" << endl;
    size_t cnt = count(vec.begin(), vec.end(), 4);
    cout << "cnt = " << cnt << endl;

    auto it = find(vec.begin(), vec.end(), 9);
    if(it == vec.end())
    {
        cout << "该元素不在vector中" << endl;
    }
    else
    {
        cout << "该元素存在vector中" << *it << endl;
    }

    cout << endl << endl << "算法中的排序函数" << endl;
    sort(vec.begin(), vec.end());
    for_each(vec.begin(), vec.end(), display);
    cout << endl;
}

int main(int argc, char **argv)
{
    test2();
    return 0;
}

